﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;

namespace suirui.ZhuMu.WebCore
{
    public class AddRequiredHeaderParameter : Swashbuckle.AspNetCore.SwaggerGen.IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();
            var attrs = context.ApiDescription.CustomAttributes();
            foreach (var attr in attrs)
            {
                // 如果 Attribute 是我们自定义的验证过滤器
                if (attr.GetType() == typeof(DataDecryptFilter))
                {
                    operation.Parameters.Add(new OpenApiParameter()
                    {
                        Name = "aeskey",
                        In = ParameterLocation.Header,
                        Required = true,
                    });
                }
            }
        }
    }
}
